//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Reflection;

namespace ComponentAutoBind
{
    /// <summary>
    /// 类型相关的实用函数。
    /// </summary>
    public static class TypeHelper
    {
        /// <summary>
        /// 获取类型名称。
        /// </summary>
        /// <param name="typeBase">基类类型。</param>
        /// <param name="assemblyNames">用于查找的程序集名称集合。</param>
        /// <returns></returns>
        public static string[] GetTypeNames(Type typeBase, string[] assemblyNames)
        {
            List<string> typeNames = new List<string>();

            foreach (string assemblyName in assemblyNames)
            {
                Assembly assembly;

                try
                {
                    assembly = Assembly.Load(assemblyName);
                }
                catch
                {
                    continue;
                }

                if (assembly == null)
                {
                    continue;
                }

                Type[] types = assembly.GetTypes();

                foreach (Type type in types)
                {
                    if (type.IsClass && !type.IsAbstract && typeBase.IsAssignableFrom(type))
                    {
                        typeNames.Add(type.FullName);
                    }
                }
            }

            typeNames.Sort();

            return typeNames.ToArray();
        }
    }
}

#endif